package web.protocols.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.ConcurrencyFailureException;
import services.ProtocolCRUDService;
import services.ProtocoloCentroService;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: scamisay
 * Date: 10/26/12
 * Time: 5:05 PM
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Protocols/SitesSelection")
public class SitesSelectionController extends ActionSupport implements ModelDriven {

    private ProtocolCRUDService protocolCRUDService;
    private ProtocoloCentroService protocoloCentroService;
    private ProtocoloCentro protocoloCentro;
    private List<ProtocoloCentro> list;
    private String errorMessage;
    private Collection<ComiteEtica> comitesC = new ArrayList<ComiteEtica>();
    private List<Involucrado> existentStaff;

    @Autowired
    public SitesSelectionController(ProtocolCRUDService protocolCRUDService,
                                    ProtocoloCentroService protocoloCentroService) {
        this.protocolCRUDService = protocolCRUDService;
        this.protocoloCentroService = protocoloCentroService;
        protocoloCentro = new ProtocoloCentro();
        list = new ArrayList<ProtocoloCentro>();
    }

    @Action(value="main", results={
            @Result(name="success", location="/protocols.sitesSelection.tiles", type="tiles")
    })
    public String execute() throws NoProtocoloCompromisopaisSelectedException {
        ProtocoloCompromisopais protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();
        list = protocoloCentroService.findAllByProtocoloCompromisopais(protocoloCompromisopais);

        if(protocoloCentro!= null && !protocoloCentro.isNew()){
            protocoloCentro = protocoloCentroService.findById(protocoloCentro.getId());
            comitesC = new ArrayList<ComiteEtica>(protocoloCentro.getComiteEticas());
        }else{
            protocoloCentro = new ProtocoloCentro();
        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction"),
            @Result(name="input", location="/protocols.sitesSelection.tiles", type="tiles")
    })
    public String save() throws NoProtocoloCompromisopaisSelectedException {
        try {
            ProtocoloCompromisopais protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();

            protocoloCentro.setProtocoloCompromisopais(protocoloCompromisopais);
            Set<ComiteEtica> comitesAux = new HashSet<ComiteEtica>(comitesC);
            protocoloCentro.setComiteEticas(comitesAux);
            protocoloCentroService.save(protocoloCentro);

        }catch (ConcurrencyFailureException cfe){
            errorMessage = "Someone has just saved the same record you did";
            execute();
            return INPUT;
        }
        return SUCCESS;
    }

    public List<ProtocoloCentro> getList() {
        return list;
    }

    public void setList(List<ProtocoloCentro> list) {
        this.list = list;
    }

    public Integer getInstitutionRequiresIECLAF(){
        if(protocoloCentro.isNew()){
            return -1;
        }else if(protocoloCentro.isInstitutionRequiresIEC()){
            return Decision.YES.getId();
        }else{
            return Decision.NO.getId();
        }
    }

    public void setInstitutionRequiresIECLAF(String id) {
        Decision institutionRequiresIECLAF = Decision.values()[Integer.parseInt(id)];
        protocoloCentro.setInstitutionRequiresIEC(institutionRequiresIECLAF.equals(Decision.YES));
    }

    public Decision[] getDecisions(){
        return Decision.values();
    }

    public Integer getPepe() {
        if(protocoloCentro.isNew()){
            return -1;
        }else if(protocoloCentro.getSeleccionado()){
            return Decision.YES.getId();
        }else{
            return Decision.NO.getId();
        }
    }

    public void setSelectedLAF(String id) {
        Decision isSelectedLAF = Decision.values()[Integer.parseInt(id)];
        protocoloCentro.setSeleccionado(isSelectedLAF.equals(Decision.YES));
    }

    public Integer getFefymLAF() {
        if(protocoloCentro.isNew()){
            return -1;
        }else if(protocoloCentro.isFefym()){
            return Decision.YES.getId();
        }else{
            return Decision.NO.getId();
        }
    }

    public void setFefymLAF(String id) {
        Decision isSelectedLAF = Decision.values()[Integer.parseInt(id)];
        protocoloCentro.setFefym(isSelectedLAF.equals(Decision.YES));
    }

    public ProtocoloCentro getProtocoloCentro() {
        return protocoloCentro;
    }

    public void setProtocoloCentro(ProtocoloCentro protocoloCentro) {
        this.protocoloCentro = protocoloCentro;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public void setSiteScoreC(String id){
        Score siteScore = Score.values()[Integer.parseInt(id)];
        protocoloCentro.setSiteScore(siteScore);
    }

    public List<Involucrado> getExistentStaff() {
        return existentStaff;
    }

    public void setExistentStaff(List<Involucrado> existentStaff) {
        this.existentStaff = existentStaff;
    }

    public Score[] getScores() {
        return Score.values();
    }

    @Override
    public Object getModel() {
        return protocoloCentro;
    }

    public Collection<ComiteEtica> getComitesC() {
        return comitesC;
    }

    public void setComitesC(Collection<ComiteEtica> comitesC) {
        this.comitesC = comitesC;
    }

    public ComiteEtica.Category[] getCategories(){
        return ComiteEtica.Category.values();
    }

    public enum Decision {
        YES(0,"Yes"),NO(1,"No");

        private Integer id;
        private String name;

        Decision(){
            this.id = -1;
            this.name = "";
        }

        Decision(Integer id,String name){
            this.id = id;
            this.name = name;
        }

        public String getName() {
            return this.name();
        }

        public Integer getId() {
            return this.id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setId(Integer id) {
            this.id = id;
        }
    }
}
